package com.situ.blog.dao.impl;

import com.situ.blog.dao.IBlogDao;
import com.situ.blog.pojo.Blog;
import com.situ.blog.pojo.query.BlogQuery;
import com.situ.blog.pojo.vo.BlogVO;
import com.situ.blog.util.JDBCUtil;
import com.situ.blog.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.*;
import java.util.*;
import java.util.Date;

public class BlogDaoImpl implements IBlogDao {
    private JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<BlogVO> selectByPage(BlogQuery blogQuery) {
        String sql = "SELECT b.*,t.`name` AS typeName FROM blog AS b INNER JOIN type AS t ON b.type_id=t.id WHERE 1=1";
        //构造一个List存所有的条件
        List<Object> queryList = new ArrayList<>();
        String queryName = blogQuery.getName();
        if (queryName != null && !"".equals(queryName)) {
            sql += " AND b.name LIKE ?";
            queryList.add("%" + queryName + "%");
        }
        sql += " LIMIT ?,?";
        int offset = (blogQuery.getPage() - 1) * blogQuery.getLimit();
        queryList.add(offset);
        queryList.add(blogQuery.getLimit());

        List<BlogVO> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(BlogVO.class), queryList.toArray());
        return list;
    }

    @Override
    public int selectTotalCount(BlogQuery blogQuery) {
        String sql = "SELECT count(*) FROM blog WHERE 1=1";
        //构造一个List存所有的条件
        List<Object> queryList = new ArrayList<>();
        String queryName = blogQuery.getName();
        if (queryName != null && !"".equals(queryName)) {
            sql += " AND name LIKE ?";
            queryList.add("%" + queryName + "%");
        }
        int totalCount = jdbcTemplate.queryForObject(sql, Integer.class, queryList.toArray());
        return totalCount;
    }

    @Override
    public void add(Blog blog) {
        String sql = "INSERT INTO blog(name, type_id,image,content) VALUES(?,?,?,?)";
        jdbcTemplate.update(sql, blog.getName(), blog.getTypeId(), blog.getImage(), blog.getContent());
    }

    @Override
    public Blog selectById(int id) {
        String sql = "SELECT * FROM blog WHERE id=?";
        Blog blog = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Blog.class), id);
        return blog;
    }

    @Override
    public void update(Blog blog) {
        String sql = "UPDATE blog SET name=?,type_id=?,image=?,content=? WHERE id=?";
        jdbcTemplate.update(sql, blog.getName(), blog.getTypeId(), blog.getImage(), blog.getContent(), blog.getId());
    }

    @Override
    public List<Blog> selectAll() {
        String sql = "SELECT * FROM blog";
        List<Blog> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Blog.class));
        return list;
    }

    @Override
    public void addExcel(Blog blog) {
        String sql = "INSERT INTO blog(name, type_id,image,content, status, is_deleted,click_count,create_time,update_time) VALUES(?,?,?,?,?,?,?,?,?)";
        jdbcTemplate.update(sql, blog.getName(), blog.getTypeId(), blog.getImage(), blog.getContent(), blog.getStatus(), blog.getDeleted(), blog.getClickCount(), blog.getCreateTime(), blog.getUpdateTime());
    }
}
